package org.ring.jb.storage;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.RandomAccessFile;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

class Item implements Serializable{
	double value;
	long count;
	void work(){double t = value*count;}
}

class Storage implements Serializable{
	List<Item> items;
	
	public Storage(){
		items = new ArrayList<Item>();
		for(int i=0; i<(86400*365); ++i){
			items.add(new Item());
		}
	}
	
	public void work(){items = null;}
}

public class ByteStorage {
	
	public static void main1(String[] args){

		long sttime = System.currentTimeMillis();
		long stmem = Runtime.getRuntime().freeMemory();
		
		Storage storage = new Storage();
		for(Item item : storage.items){item.work();};
		
		System.out.println((long)(Runtime.getRuntime().freeMemory() - stmem));
		System.out.println((long)(System.currentTimeMillis() - sttime));
		
		storage.work();
	}
	
	public static void main2(String[] args) throws IOException{			

		long sttime = System.currentTimeMillis();
		long stmem = Runtime.getRuntime().freeMemory();
		
		Storage storage = new Storage();
		//for(Item item : storage.items){item.work();};
		
		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("C:\\Develop\\a.txt"));
		oos.writeObject(storage);
		oos.flush();
		oos.close();
		System.out.println((long)(Runtime.getRuntime().freeMemory() - stmem));
		System.out.println((long)(System.currentTimeMillis() - sttime));
		
		storage.work();
	}
	
	public static void main5(String[] args) throws IOException{			

		long sttime = System.currentTimeMillis();
		long stmem = Runtime.getRuntime().freeMemory();
		
		Storage storage = new Storage();
		//for(Item item : storage.items){item.work();};
		
		FileOutputStream fos = (new FileOutputStream("C:\\Develop\\a.txt"));
		byte[] b = new byte[16];
		for(Item item: storage.items){
			fos.write(b);
		}
		fos.flush();
		fos.close();
		System.out.println((long)(Runtime.getRuntime().freeMemory() - stmem));
		System.out.println((long)(System.currentTimeMillis() - sttime));
		
		storage.work();
	}
	
	public static void main(String[] args)throws IOException{

		long sttime = System.currentTimeMillis();
		long stmem = Runtime.getRuntime().freeMemory();
		
		Storage storage = new Storage();
		//for(Item item : storage.items){item.work();};
		
		RandomAccessFile raf = new RandomAccessFile("C:\\Develop\\a.txt", "rw");
		byte[] b = new byte[16];
		for(Item item: storage.items){
			raf.write(b);
		}
		raf.close();
		System.out.println((long)(Runtime.getRuntime().freeMemory() - stmem));
		System.out.println((long)(System.currentTimeMillis() - sttime));
		
		storage.work();
	}

	
	public static void main6(String[] args)throws IOException{
		long sttime = System.currentTimeMillis();
		long stmem = Runtime.getRuntime().freeMemory();
		
		Storage storage = new Storage();
		//for(Item item : storage.items){item.work();};
		
		BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("C:\\Develop\\a.txt"));
		byte[] b = new byte[16];
		for(Item item: storage.items){
			bos.write(b);
		}
		bos.flush();
		bos.close();
		System.out.println((long)(Runtime.getRuntime().freeMemory() - stmem));
		System.out.println((long)(System.currentTimeMillis() - sttime));
		
		storage.work();
	}
	

	public static void main7(String[] args)throws IOException{

		long sttime = System.currentTimeMillis();
		long stmem = Runtime.getRuntime().freeMemory();
		
		Storage storage = new Storage();
		//for(Item item : storage.items){item.work();};
		
		ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("C:\\Develop\\a.txt")));
		oos.writeObject(storage);
		oos.flush();
		oos.close();
		System.out.println((long)(Runtime.getRuntime().freeMemory() - stmem));
		System.out.println((long)(System.currentTimeMillis() - sttime));
		
		storage.work();
	}
	
	public static void main8(String[] args)throws IOException{

		long sttime = System.currentTimeMillis();
		long stmem = Runtime.getRuntime().freeMemory();
		
		Storage storage = new Storage();
		//for(Item item : storage.items){item.work();};
		
		RandomAccessFile raf = new RandomAccessFile("C:\\Develop\\a.txt", "rw");
		byte[] b = new byte[16];
		for(int i=0; i<86400*300; ++i){
			raf.write(b);
		}
		System.out.println((long)(Runtime.getRuntime().freeMemory() - stmem));
		System.out.println((long)(System.currentTimeMillis() - sttime));
		

		for(int i=0; i<10000; ++i){
			raf.seek(i*3);
			raf.write(1);
			System.out.println(raf.read());
		}
		raf.close();

		System.out.println((long)(Runtime.getRuntime().freeMemory() - stmem));
		System.out.println((long)(System.currentTimeMillis() - sttime));
		
		storage.work();
	}
}
